const { MessageType } = require("../../api/type/MessageType");
const { ServerGetUserSockIdByID } = require("../../server/user/index.js");

module.exports = (io, socket) => {
    // 处理语音通话请求
    socket.on(MessageType.VOICECALL_REQUEST, async (data) => {
        const { to, from, username } = data;
        // 获取接收方的 socketid
        const socketRes = await ServerGetUserSockIdByID(to);
        if (socketRes.isok && socketRes.data.socketid) {
            // 向接收方发送通话请求
            io.to(socketRes.data.socketid).emit(MessageType.VOICECALL_REQUEST, {
                from,
                username
            });
            console.log(`已向用户 ${to} 发送语音通话请求，socketid: ${socketRes.data.socketid}`);
        } else {
            // 通知发起方，对方不在线
            socket.emit("callError", {
                message: "对方不在线，无法建立通话"
            });
            console.log(`用户 ${to} 不在线，无法发送语音通话请求`);
        }
    });

    // 处理接受通话
    socket.on(MessageType.VOICECALL_ACCEPT, async (data) => {
        const { to, from } = data;
        // 获取接收方的 socketid
        const socketRes = await ServerGetUserSockIdByID(to);
        if (socketRes.isok && socketRes.data.socketid) {
            // 通知发起方，通话已被接受
            io.to(socketRes.data.socketid).emit(MessageType.VOICECALL_ACCEPT, {
                from
            });
            console.log(`用户 ${from} 接受了来自用户 ${to} 的通话请求`);
        }
    });

    // 处理拒绝通话
    socket.on(MessageType.VOICECALL_REJECT, async (data) => {
        const { to, from } = data;
        // 获取接收方的 socketid
        const socketRes = await ServerGetUserSockIdByID(to);
        if (socketRes.isok && socketRes.data.socketid) {
            // 通知发起方，通话已被拒绝
            io.to(socketRes.data.socketid).emit(MessageType.VOICECALL_REJECT, {
                from
            });
            console.log(`用户 ${from} 拒绝了来自用户 ${to} 的通话请求`);
        }
    });

    // 处理 WebRTC offer
    socket.on(MessageType.VOICECALL_OFFER, async (data) => {
        const { to, from, offer } = data;
        // 获取接收方的 socketid
        const socketRes = await ServerGetUserSockIdByID(to);
        if (socketRes.isok && socketRes.data.socketid) {
            // 转发 offer 给接收方
            io.to(socketRes.data.socketid).emit(MessageType.VOICECALL_OFFER, {
                from,
                offer
            });
            console.log(`已向用户 ${to} 发送 offer`);
        }
    });

    // 处理 WebRTC answer
    socket.on(MessageType.VOICECALL_ANSWER, async (data) => {
        const { to, from, answer } = data;
        // 获取接收方的 socketid
        const socketRes = await ServerGetUserSockIdByID(to);
        if (socketRes.isok && socketRes.data.socketid) {
            // 转发 answer 给呼叫方
            io.to(socketRes.data.socketid).emit(MessageType.VOICECALL_ANSWER, {
                from,
                answer
            });
            console.log(`已向用户 ${to} 发送 answer`);
        }
    });

    // 处理 ICE 候选
    socket.on(MessageType.VOICECALL_ICE, async (data) => {
        const { to, from, candidate } = data;
        // 获取接收方的 socketid
        const socketRes = await ServerGetUserSockIdByID(to);
        if (socketRes.isok && socketRes.data.socketid) {
            // 转发 ICE 候选给对方
            io.to(socketRes.data.socketid).emit(MessageType.VOICECALL_ICE, {
                from,
                candidate
            });
        }
    });

    // 处理挂断通话
    socket.on(MessageType.VOICECALL_HANGUP, async (data) => {
        const { to, from } = data;
        console.log(`通话挂断: 用户 ${from} 挂断了与用户 ${to} 的通话`);

        // 获取接收方的 socketid
        const toSocketRes = await ServerGetUserSockIdByID(to);
        // 获取发送方的 socketid
        const fromSocketRes = await ServerGetUserSockIdByID(from);

        // 通知接收方（对方）通话结束
        if (toSocketRes.isok && toSocketRes.data.socketid) {
            io.to(toSocketRes.data.socketid).emit(MessageType.VOICECALL_HANGUP, {
                from
            });
            console.log(`已向用户 ${to} 发送挂断通知`);
        }

        // 确保发送方也收到通话结束的消息（用于记录通话记录）
        if (fromSocketRes.isok && fromSocketRes.data.socketid) {
            io.to(fromSocketRes.data.socketid).emit(MessageType.VOICECALL_HANGUP, {
                from: to // 这里是为了通知自己，所以发送者ID用对方的ID
            });
            console.log(`已向用户 ${from} 发送挂断通知`);
        }
    });
}; 